// pages/logs/get-pwd/index.js
const App = getApp();
import { hexMD5 } from "../../../utils/md5.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tel: '',
    verifyCode: '',
    password: '',
    password1: '',
    verifyCodeNumber: 0,
    isVerify: false
  },

  yesPad() {
    if (!this.data.tel) {
      App.prompts('请输入手机号码');
      return false;
    }
    if (!this.data.verifyCode) {
      App.prompts('请输入验证码');
      return false;
    }
    if (!this.data.password || !this.data.password1) {
      App.prompts('请输入密码');
      return false;
    }
    if (this.data.password !== this.data.password1) {
      App.prompts('两次密码输入不一致，请重新输入');
      return false;
    }
    let url = 'mp/user/retrievePassword';
    let param = {
      tel: this.data.tel,
      password: hexMD5(this.data.password),
      verifyCode: this.data.verifyCode
    };
    App.ajax(url, 'POST', param).then(res => {
      console.log('忘记密码', res)
      wx.navigateTo({
        url: '/pages/logs/logs',
      })
    }).catch(err => {
      console.log(err)
    })
  },

  obtainCode() {
    if (!this.data.tel) {
      App.prompts('请输入手机号码');
      return false;
    }
    let url = 'open/sms/sendVerifyCode';
    App.ajax(url, 'POST', {tel: this.data.tel}).then(res => {
      console.log(res);
      this.setData({
        isVerify: true,
        verifyCodeNumber: 60
      })
      this.verifyFun();
    }).catch(err => {
      console.log(err)
    })
  },
  verifyFun() {
    setTimeout(() => {
      if (this.data.verifyCodeNumber > 0) {
        this.setData({
          verifyCodeNumber: this.data.verifyCodeNumber -= 1
        })
        this.verifyFun();
      }
    }, 1000)
  },

  bindKeyInput(e) {
    let key = e.currentTarget.dataset.key;
    let date = {};
    date[key] = e.detail.value;
    this.setData(date);
  },

})